﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fashion.Models;
using Fashion.Models.Interface;
using Fashion.Models.Model;

namespace Fashion.Controllers
{
    public class PropertyController : Controller
    {
        private IProductCategoryRepository _repository;

        public PropertyController()
        {
            _repository = new ProductCategoryRepository();
        }

        #region 
        
        public ActionResult Index()
        {
            return View(_repository.GetAllPropertyName());
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(PropertyNameModel model)
        {
            if (!ModelState.IsValid)
                return View(model);
            var propertyName = new ProductPropertyName()
                                   {
                                       Id = 0,
                                       Name =  model.Name,
                                       Unit =  model.Unit,
                                       IsValueStatic = model.IsValueStatic,
                                       IsMultiValue = model.IsMultiValue,
                                   };
            _repository.AddNewPropertyName(propertyName);
            return RedirectToAction("Index");
        }

        public ActionResult Update(int id)
        {
            var propertyName = _repository.GetPropertyNameById(id);
            var model = new PropertyNameModel()
                            {
                                Id =  propertyName.Id,
                                Name = propertyName.Name,
                                Unit = propertyName.Unit,
                                IsValueStatic = propertyName.IsValueStatic,
                                IsMultiValue = propertyName.IsMultiValue,
                            };
            return View(model);
        }

        [HttpPost]
        public ActionResult Update(PropertyNameModel model)
        {
            if (!ModelState.IsValid)
                return View(model);
            var propertyName = _repository.GetPropertyNameById(model.Id);
            propertyName.Name = model.Name;
            propertyName.Unit = model.Unit;
            propertyName.IsValueStatic = model.IsValueStatic ;
            propertyName.IsMultiValue = model.IsMultiValue;
            _repository.Update();
            return RedirectToAction("Index");
        }

        public ActionResult Delete(int id)
        {
            _repository.DeletePropertyName(id);
            return RedirectToAction("Index");
        }

        #endregion

        #region Value Static

        public  ActionResult AllValueStatic(int propertyId)
        {
            var property = _repository.GetPropertyNameById(propertyId);
            return View(property.ProductPropertyValueStatics);
        }

        public ActionResult AddValueStatic(int propertyId)
        {
            var model = new PropertyValueStaticModel()
                            {
                                Id=0,
                                PropertyId = propertyId,
                                Active = true,
                            };
            return View(model);
        }

        [HttpPost]
        public ActionResult AddValueStatic(PropertyValueStaticModel model)
        {
            if (ModelState.IsValid)
            {
                var valueStatic = new ProductPropertyValueStatic()
                                      {
                                          PropertyId = model.PropertyId,
                                          Value = model.Value,
                                          Active =  true,
                                      };
                _repository.AddNewValueStatic(valueStatic);
            }
            return RedirectToAction("AllValueStatic", new { propertyId = model.PropertyId });
        }

        public ActionResult DeleteValueStatic(int id, int propertyId)
        {
            _repository.DeleteValueStatic(id);
            return RedirectToAction("AllValueStatic", new { propertyId = propertyId});
        }

        public ActionResult EditValueStatic(int id)
        {
            var valueStatic = _repository.GetPropertyValueStaticById(id);
            var model = new PropertyValueStaticModel()
                            {
                                Id=valueStatic.Id,
                                PropertyId =  valueStatic.PropertyId,
                                Value =  valueStatic.Value,
                                Active =  valueStatic.Active,
                            };
            return View(model);
        }

        [HttpPost]
        public ActionResult EditValueStatic(PropertyValueStaticModel model)
        {
            if (!ModelState.IsValid)
                return View(model);
            var valueStatic = _repository.GetPropertyValueStaticById(model.Id);
            valueStatic.Value = model.Value;
            valueStatic.Active = model.Active;
            _repository.Update();
            return RedirectToAction("AllValueStatic", new {model.PropertyId});
        }

        #endregion
    }
}
